from datetime import datetime

from django.db import models
from user.models import Users
from xm3 import settings


# Create your models here.
class City(models.Model):
    """城市表"""
    name = models.CharField(max_length=100)

    class Meta:
        db_table = 'city'


class Attractions(models.Model):
    """景点表"""
    name = models.CharField(max_length=100)
    img = models.ImageField(upload_to='img', null=True, blank=True)
    city = models.ForeignKey(City, blank=True, null=True, on_delete=models.SET_NULL)
    desc = models.CharField(max_length=1000, blank=True, null=True)
    price = models.IntegerField(default=0)
    type = models.CharField(max_length=100, blank=True, null=True)
    num = models.IntegerField(default=1)

    class Meta:
        db_table = 'attractions'


class UserScenic(models.Model):
    """用户浏览景点表"""
    user = models.ForeignKey(Users, on_delete=models.CASCADE)
    attraction = models.ForeignKey(Attractions, on_delete=models.CASCADE)
    visit_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "user_scenic"


class UserComment(models.Model):
    """用户评论表"""
    user = models.ForeignKey(Users, on_delete=models.CASCADE)
    attraction = models.ForeignKey(Attractions, on_delete=models.CASCADE)
    comment = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL)
    content = models.CharField(max_length=1000,verbose_name='评论内容')
    comment_time = models.DateTimeField(auto_now_add=True)
    score = models.IntegerField(default=0)
    is_good = models.BooleanField(default=False)
    image = models.CharField(max_length=1000, blank=True, null=True)

    class Meta:
        db_table = "user_comment"


class AttractionTicke(models.Model):
    """景点门票表"""
    attraction = models.ForeignKey(Attractions, on_delete=models.CASCADE)
    name = models.CharField(max_length=50, verbose_name='名称')
    price = models.IntegerField(default=0, verbose_name='价格')
    num = models.IntegerField(default=100, verbose_name='库存')

    class Meta:
        db_table = 'attraction_ticket'


from tools.snowflake import SnowFlake


class ScenicSpot(models.Model):
    """景点攻略表"""
    id = models.BigAutoField(primary_key=True)  # 使用雪花算法生成
    name = models.CharField(max_length=100)
    province = models.CharField(max_length=20)
    tags = models.JSONField()  # 存储景点标签
    recommend_time = models.CharField(max_length=50)  # 推荐时间段

    def save(self, *args, **kwargs):
        if not self.id:
            snowflake = SnowFlake(machine_id=settings.MACHINE_ID)
            self.id = snowflake.generate_id()
        super().save(*args, **kwargs)

    class Meta:
        db_table = 'scenic_spot'


class UserFavorite(models.Model):
    """用户看过"""
    user = models.ForeignKey(Users, on_delete=models.CASCADE)
    spot = models.ForeignKey(ScenicSpot, on_delete=models.CASCADE)
    rating = models.FloatField(blank=True, null=True)

    class Meta:
        db_table = 'user_favorite'
